package training;

import java.io.IOException;
//import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FilmsServlet
 */
public class FilmsServlet2 extends HttpServlet {
	private static final String PARAM_NAME = "Name";
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handle(request, response);

	}

	void handle(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final String parameterName = request.getParameter(PARAM_NAME);
		List<Film> films = getFilms(parameterName);
		
		request.setAttribute("films", films);
		request.getRequestDispatcher("films.jsp").forward(request, response);
	}

	private List<Film> getFilms(String parameter) {
		final List<Film> list = new ArrayList<Film>();
		list.add(new Film("Iron man 3", "action"));
		list.add(new Film("Scary MoVie 5", "comedy"));
		list.add(new Film("Pain & Gain", "comedy, action"));
		list.add(new Film("The Great Gatsby ", "drama"));
		return list;
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handle(request, response);
	}

}
